import { Channels } from './channel';
let _Channels = Channels;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ChannelMock from './mallm/pub/channelMock';
 * ChannelMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/channel/listKey').reply(config => {
      let params = config.params;
      let mockChannels = _Channels.filter(channel => {
      	if(params.id||params.id==''){ if( channel.id.indexOf(params.id)>=0) return true;}
      	if(params.name||params.name==''){ if( channel.name.indexOf(params.name)>=0) return true;}
      	if(params.url||params.url==''){ if( channel.url.indexOf(params.url)>=0) return true;}
      	if(params.iconUrl||params.iconUrl==''){ if( channel.iconUrl.indexOf(params.iconUrl)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( channel.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( channel.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockChannels
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/channel/list').reply(config => {
      let params = config.params;
      let mockChannels = _Channels.filter(channel => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(channel.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(channel.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.url||params.url==''){if(channel.url.indexOf(params.url)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.iconUrl||params.iconUrl==''){if(channel.iconUrl.indexOf(params.iconUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(channel.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(channel.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockChannels
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockChannels.length;
      mockChannels = mockChannels.filter((channel, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockChannels,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/channel/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Channels = _Channels.filter(channel => channel.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/channel/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Channels = _Channels.filter(channel => !params.includes(channel.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/channel/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,url,iconUrl,sortOrder,deptid } = params;
      _Channels.some(channel => {
        if (channel.id === id) {
			channel.id = id;
			channel.name = name;
			channel.url = url;
			channel.iconUrl = iconUrl;
			channel.sortOrder = sortOrder;
			channel.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/channel/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,url,iconUrl,sortOrder,deptid } = params;
      _Channels.push({
		id:id,
		name:name,
		url:url,
		iconUrl:iconUrl,
		sortOrder:sortOrder,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};